package com.myapp.ruyixing.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.myapp.ruyixing.model.FoodDetail;
import com.myapp.ruyixing.model.FoodDetail.DetailItem;

public class FoodDetailParser extends RYJSONParser {
	@Override
	public Object parse(JSONObject jsonObject) {

		FoodDetail detail = new FoodDetail();
		detail.list = new ArrayList<FoodDetail.DetailItem>();

		ModelParser.parseModel(detail, jsonObject);

		JSONObject data = jsonObject.optJSONObject("data");
		detail.price = data.optString("price", "");
		detail.foodname = data.optString("foodname", "");
		detail.taste = data.optString("taste", "");
		detail.favorite = data.optInt("favorite", 0);
		detail.distance = data.optLong("distance", 0);
		detail.score = data.optInt("score", 0);
		detail.picurl = data.optString("picurl", "");
		detail.lat = data.optDouble("lat", 0.0);
		detail.lon = data.optDouble("lon", 0.0);
		detail.shopid = data.optString("shopid", "");
		detail.shopname = data.optString("shopname", "");
		detail.desc = data.optString("desc", "");

		JSONArray list = data.optJSONArray("list");

		for (int i = 0; list != null && i < list.length(); i++) {
			JSONObject object = list.optJSONObject(i);
			if (object != null) {

				DetailItem item = new DetailItem();
				item.content = object.optString("content", "");
				item.title = object.optString("title", "");
				item.type = object.optString("type", "");
				item.datetime = object.optString("datetime", "");
				item.score = object.optInt("score", 0);
				item.total = object.optInt("total", 0);
				item.id = object.optString("id", "");
				item.username = object.optString("username", "");
				item.pics = new ArrayList<String>();
				JSONArray array = object.optJSONArray("pics");
				if (array != null && array.length() > 0) {
					for (int j = 0; j < array.length(); j++) {
						String picurl = array.optString(j, "");
						item.pics.add(picurl);
					}
				}
				detail.list.add(item);
			}
		}

		return detail;

	}
}
